package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BSulfurEmission;
import com.isdepci.project.carbon.domain.BSulfurEmissionParameter;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BSulfurEmissionMapper;
import com.isdepci.project.carbon.mapper.BSulfurEmissionParameterMapper;
import com.isdepci.project.carbon.service.IBSulfurEmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 脱硫排放Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BSulfurEmissionServiceImpl implements IBSulfurEmissionService
{
    @Autowired
    private BSulfurEmissionMapper bSulfurEmissionMapper;
    @Autowired
    private BSulfurEmissionParameterMapper sulfurEmissionParameterMapper;


    @Override
    public BSulfurEmissionParameter getSulfurEmissionParameters(Integer desulfurizerType) {
        return sulfurEmissionParameterMapper.findByDesulfurizerType(desulfurizerType);
    }

    @Override
    public AjaxResult calSulfurEmission(Long projectId) {
        // 查询出所有的BSulfurEmission数据
        List<BSulfurEmission> bSulfurEmissions = bSulfurEmissionMapper.selectBSulfurEmissionByProjectId(projectId);
        // 初始化总的脱硫量
        BigDecimal totalDesulfurization = BigDecimal.ZERO;
        // 遍历每一条数据，计算脱硫量
        for (BSulfurEmission bSulfurEmission : bSulfurEmissions) {
            // 计算排放因子 (EF)
            BigDecimal EF = bSulfurEmission.getEmissionFactor()
                    .multiply(bSulfurEmission.getConversionRate())
                    .divide(new BigDecimal("100"), 4, BigDecimal.ROUND_HALF_UP);  // 除以100转换为小数
            // 计算消耗量调整 (CAL)
            BigDecimal CAL = bSulfurEmission.getConsumption()
                    .multiply(bSulfurEmission.getCarbonateContent());
            // 计算脱硫量 = EF * CAL
            BigDecimal desulfurization = EF.multiply(CAL);
            bSulfurEmission.setCarbonEmissions(desulfurization);
            bSulfurEmissionMapper.updateBSulfurEmission(bSulfurEmission);
            // 累加脱硫量
            totalDesulfurization = totalDesulfurization.add(desulfurization);
        }
        return AjaxResult.success(totalDesulfurization.setScale(2, RoundingMode.HALF_UP));
    }

    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BSulfurEmission> list =  excel.getSulfurEmissionList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bSulfurEmissionMapper.insertList(list);
        }
        return AjaxResult.success();
    }


    /**
     * 查询脱硫排放
     */
    @Override
    public BSulfurEmission selectBSulfurEmissionById(Long id)
    {
        return bSulfurEmissionMapper.selectBSulfurEmissionById(id);
    }

    /**
     * 查询脱硫排放列表
     */
    @Override
    public List<BSulfurEmission> selectBSulfurEmissionList(BSulfurEmission bSulfurEmission)
    {
        return bSulfurEmissionMapper.selectBSulfurEmissionList(bSulfurEmission);
    }

    /**
     * 新增脱硫排放
     */
    @Override
    public int insertBSulfurEmission(BSulfurEmission bSulfurEmission)
    {
        return bSulfurEmissionMapper.insertBSulfurEmission(bSulfurEmission);
    }

    /**
     * 修改脱硫排放
     */
    @Override
    public int updateBSulfurEmission(BSulfurEmission bSulfurEmission)
    {
        return bSulfurEmissionMapper.updateBSulfurEmission(bSulfurEmission);
    }

    /**
     * 批量删除脱硫排放
     */
    @Override
    public int deleteBSulfurEmissionByIds(Long[] ids)
    {
        return bSulfurEmissionMapper.deleteBSulfurEmissionByIds(ids);
    }

    /**
     * 删除脱硫排放信息
     */
    @Override
    public int deleteBSulfurEmissionById(Long id)
    {
        return bSulfurEmissionMapper.deleteBSulfurEmissionById(id);
    }


}
